package com.tjetc.usermanage;

import com.tjetc.domian.Maintenancerequest;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.MaintenanceService;
import com.tjetc.vo.Result;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class MaintenanceView {
    private Scanner scanner = new Scanner(System.in);
    private MaintenanceService maintenanceService = ClientProxy.getProxyInstance(MaintenanceService.class);
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    public void start() {
        while (true) {
            System.out.println(" 维修服务菜单");
            System.out.println("1.报修");
            System.out.println("2.查看报修记录");
            System.out.println("0.返回上一级");
            switch (scanner.nextInt()) {
                case 1:
                    add();
                    break;
                case 2:
                    select();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }
    }

    private void select() {
        System.out.println("查看报修记录");
        System.out.println("请输入居民id");
        int residentid = scanner.nextInt();
        List<Maintenancerequest> maintenancerequests = maintenanceService.select(residentid);
        for (Maintenancerequest maintenancerequest : maintenancerequests) {
            System.out.println(maintenancerequest);
        }
    }

    private void add() {
        System.out.println("报修");
        System.out.println("请输入居民id");
        int residentid = scanner.nextInt();
        System.out.println("请输入公寓id");
        int apartmentid = scanner.nextInt();
        System.out.println("请输入报修日期 (格式: yyyy-MM-dd)");
        String requestdate = scanner.next();
        System.out.println("请输入报修时间(格式: HH:mm:ss)");
        String requesttime = scanner.next();
        System.out.println("请输入报修描述");
        String description = scanner.next();
        try {
            Date date = dateFormat.parse(requestdate);
            Date time = timeFormat.parse(requesttime);
            Maintenancerequest maintenancerequest = new Maintenancerequest();
            maintenancerequest.setResidentid(residentid);
            maintenancerequest.setApartmentid(apartmentid);
            maintenancerequest.setRequestdate(date);
            maintenancerequest.setRequesttime(time);
            maintenancerequest.setDescription(description);
            maintenancerequest.setStatus("待处理");
            Result result = maintenanceService.add(maintenancerequest);
            System.out.println(result.getMsg());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
